//地址列表
<template>
  <div>
    <van-nav-bar
      title="确认收货地址"
      placeholder
      fixed
      left-arrow
      @click-left="goBack"
    />
    <van-address-list
      v-model="AddressId"
      :list="list"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
    />
  </div>
</template>
 
<script>
export default {
  data() {
    return {
      uname: sessionStorage.getItem("uname"), //获取用户名
      AddressId: "1",
      list: [],
      data: null,
    };
  },
  methods: {
    //返回me页面
    goBack() {
      this.$router.push("/me");
    },
    //点击新增按钮时触发
    onAdd() {
      this.$router.push("/address");
    },

    //点击编辑传地址id给地址编辑，以便地址回显
    onEdit(item, index) {
      let id = this.list[index].id;
      this.$router.push(`address?id=${id}`);
    },
  },
  mounted() {
    //获取用户数据
    this.axios.get("/address/getAddressId").then((res) => {
      console.log(res.data.data);
      this.list = res.data.data;
      console.log(this.list);
    });
    //发送请求获取地址
    const url = "/address/address";
    const params = `uname=${this.uname}`;
    this.axios.post(url, params).then((res) => {
      this.list = res.data.results;
      //选中地址列表的第一个地址
      if (this.list.length != 0) {
        this.chosenAddressId = this.list[0].id;
      }
      console.log(this.list);
    });
  },
};
</script>
 
 